package com.yboot.starter.mybatis.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.yboot.starter.common.core.security.UserContext;
import com.yboot.starter.mybatis.core.handle.BaseEntityInnerMetaObjectHandler;
import com.yboot.starter.mybatis.core.handle.InnerMetaObjectHandler;
import com.yboot.starter.mybatis.core.handle.MybatisPlusMetaObjectHandler;
import com.yboot.starter.mybatis.core.plugin.PaginationInnerExtendInterceptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 配置类
 *
 * @author yangcs
 * @date 2025/8/15
 */
@Configuration
@EnableConfigurationProperties(BaseColumnProperties.class)
public class MybatisPlusAutoConfiguration {

    @Bean
    public BaseEntityInnerMetaObjectHandler baseEntityInnerMetaObjectHandler(BaseColumnProperties properties, UserContext userContext) {
        return new BaseEntityInnerMetaObjectHandler(properties, userContext);
    }

    @Bean
    public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler(List<InnerMetaObjectHandler> innerMetaObjectHandlers) {
        return new MybatisPlusMetaObjectHandler(innerMetaObjectHandlers);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerExtendInterceptor());
        // 分页插件
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        // 防止全表更新与删除
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }
}
